package com.kingdee.shr.custom.funservice;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import com.kingdee.bos.BOSException;
import com.kingdee.shr.compensation.CalSchemeInfo;

@Deprecated
public class CalFunctionServiceExt {
    public double getBusCostCoutData(String type, java.util.Map paramMap, java.util.HashMap tempMap,
            com.kingdee.shr.compensation.CalSchemeInfo calScheme, String mainTableRowFilter) {
        com.kingdee.bos.Context ctx = com.kingdee.shr.compensation.app.formula.data.Tools.getInstance()
                .getCtx();
        // 定义唯一key值
        String key = "getBusCostCoutData" + calScheme.getNumber() + calScheme.getPeriodYear()
                + calScheme.getPeriodMonth();
        java.util.Map<String, Double> dataMap = new java.util.HashMap<String, Double>();
        String personId = paramMap.get("T_HR_SCMPCALTABLE_FPERSONID") == null ? null : paramMap.get(
                "T_HR_SCMPCALTABLE_FPERSONID").toString();
        double sale = 0.00;
        if (personId == null)
            return 0;

        // 从数据库中取值初始化tempMap
        if (null == tempMap || null == tempMap.get(key)) {
            String productType;
            String sqlStr = getBusCostCoutDataSql(personId, mainTableRowFilter, calScheme);
            try {
                com.kingdee.jdbc.rowset.IRowSet rowSet = com.kingdee.eas.util.app.DbUtil.executeQuery(ctx,
                        sqlStr);
                while (rowSet.next()) {
                    personId = rowSet.getString("FPersonId");
                    productType = rowSet.getString("FType");
                    sale = rowSet.getDouble("FSale");
                    dataMap.put(personId + "_" + productType, sale);
                }
            } catch (BOSException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            tempMap.put(key, dataMap);
        }

        // 从初始化完成的tempMap中取值
        dataMap = (java.util.Map<String, Double>) tempMap.get(key);
        return dataMap.get(personId + "_" + type) == null ? 0.00 : dataMap.get(personId + "_" + type);
    }

    private String getBusCostCoutDataSql(String personId, String mainTableRowFilter, CalSchemeInfo calScheme) {
        // mainTableRowFilter 格式：t_hr_scmpcaltable.fid in (select FID from T_HR_SCmpCalTable where
        // FCalState in (1,2,3,4,5,6,7)
        // and FID in (select * from VTZD0RDVX15O2LMJ0F0TWRZ7UYW8YKVSD8D))
        StringBuffer sqlStr = new StringBuffer();
        sqlStr.append(" select FPersonId, FType, FSale from T_HR_SPerformanceExt where ");
        if (null == mainTableRowFilter) {
            sqlStr.append(" FPersonId = '").append(personId).append("' ");
        } else {
            sqlStr.append(" FPersonId in (select FPersonId from t_hr_scmpcaltable ");
            sqlStr.append(" where ").append(mainTableRowFilter);
            sqlStr.append(")");
        }
        sqlStr.append(" and FPeriodYear = ").append(calScheme.getPeriodYear());
        sqlStr.append(" and FPeriodMonth = ").append(calScheme.getPeriodMonth());
        return sqlStr.toString();
    }

}
